package org.evomaster.resource.rest.generator

import org.evomaster.resource.rest.generator.model.RestMethod
import org.evomaster.resource.rest.generator.model.StrategyNameResource
import org.evomaster.resource.rest.generator.pom.DependencyManager
import org.evomaster.resource.rest.generator.pom.PackagedPOModel

/**
 * created by manzh on 2020-04-22
 */
object ArchiveExp {

    const val groupId = "evo.artificial.archive.exp"

    fun generate(node: Int,numOfProperty: Int, numOfImpact : Int, branchPerImpact: Int) : GenConfig{
        if (node < 1)
            throw IllegalArgumentException("node should be more than 1, but $node")
        if (numOfProperty < numOfImpact)
            throw IllegalArgumentException("numOfImpact should not be more than numOfProperty")
        val config = GenConfig()

        config.numOfNodes = node
        config.numOfOneToOne = 1
        config.groupId = ResExp.groupId
        config.outputType = GenConfig.OutputType.MAVEN_PROJECT
        config.outputContent = GenConfig.OutputContent.CS_EM_EX
        config.nameStrategy = StrategyNameResource.RAND_FIXED_LENGTH

        config.numOfExtraProperties = numOfProperty
        config.numOfImpactProperties = numOfImpact
        config.branchesForImpact = branchPerImpact

        config.restMethods = listOf(RestMethod.POST_ID, RestMethod.DELETE, RestMethod.GET_ID)

        config.projectName = "n$node-prop$numOfProperty-iprop$numOfImpact-branch$numOfImpact"

        setCSEMEX(config)

        return config
    }

    private fun setCSEMEX(config: GenConfig){
        config.csName = "${config.projectName}-cs"
        config.emName = "${config.projectName}-em"
        config.exName = "${config.projectName}-ex"
        val suf = config.projectName.toLowerCase().replace("-", "")
        config.csProjectPackage="org.autogenerated.rest.artificial.cs.$suf"
        config.emProjectPackage="em.resource.exp.artificial.controller.$suf"
        config.exProjectPackage="ex.resource.exp.artificial.controller.$suf"
    }
}

fun main(args : Array<String>){

    val parent = "archive-selection-exp-v0"
    val evomaster_version = "1.0.2-SNAPSHOT"
    val branchPerImpact = 4

    DependencyManager.defined_version.replace(DependencyManager.EVOMASTER_CLIENT_JAVA_INSTRUMENTATION.versionKey, evomaster_version)

    val exp = arrayOf(3,5).flatMap { n->
        listOf(Pair(10,1), Pair(10, 5), Pair(10,9)).map { pair ->
            ArchiveExp.generate(n, pair.first, pair.second, branchPerImpact)
        }
    }

    val folder  = "/Users/mazh001/Documents/GitHub/ap/$parent"
    val expFolder = PackagedPOModel(modules = exp.map { it.projectName }, groupId = ResExp.groupId, artifactId = parent, output = folder)
    expFolder.save()

    exp.forEach {
        it.outputFolder = folder
        it.parents = listOf(parent)
        GenerateREST(it).run()
    }

    Util.generateDeployScript(exp, evomaster_version, folder)

}